import Qs from 'qs'
import Axios from 'axios'
import router from '../router'
import { store } from '../store/store'

// 登录请求页面
export const requestLogin = (url, params) => {
  return new Promise((resolve, reject) => {
    Axios.post(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
// 菜单请求
export const requestMenu = url => {
  return new Promise((resolve, reject) => {
    Axios.post(url)
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
//组织机构Get请求
export const requestGetOrg = (url,params) =>{
  return new Promise((resolve,reject) =>{
    Axios.get(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
//组织机构Post请求
export const requestPostOrg = (url,params) =>{
  return new Promise((resolve,reject) =>{
    Axios.post(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
//字典Get请求
export const requestGetDict = (url,params) =>{
  return new Promise((resolve,reject) =>{
    Axios.get(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
//字典Post请求
export const requestPostDict = (url,params) =>{
  return new Promise((resolve,reject) =>{
    Axios.post(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}
//设备Post请求
export const requestPostMachine = (url,params) =>{
  return new Promise((resolve,reject) =>{
    Axios.post(url, Qs.stringify(params))
    .then(res => {
      resolve(res.data)
    })
    .catch(error => {
      reject(error)
    })
})
}
//Data Aquisition表格页面请求
export const DataTableRequest = url => {
  return new Promise((resolve, reject) => {
    Axios.post(url)
      .then(res => {
        resolve(res.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

// 响应拦截器
Axios.interceptors.response.use(
  response => {
    // console.log(response)
    return Promise.resolve(response)
  },
  error => {
    // console.log('interceptors',error)
    if (store.state.mode === 'dev') {
    } else {
      if (error && error.response) {
        switch (error.response.status) {
          case 400:
            error.message = '错误请求，用户名或密码错误'
            break
          case 401:
            router.replace({
              path: '/login',
              // query: ''
            })
            break
          default:
            error.message = '连接错误'
        }
      }
      return Promise.reject(error)
    }
  }
)

// 请求拦截器
Axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('access_token')) {
      config.headers['Authorization'] =
        'Bearer ' + localStorage.getItem('access_token')
    } else {
      console.log('no token')
    }
    return config
  },
  error => {}
)
